package com.bauer.mq.rabbitmq.listener;

import com.rabbitmq.client.Channel;
import java.io.IOException;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

/**
 * @description:
 * @author: weirui
 * @create: 2020-09-22 15:19
 **/
@Component
public class MyRabbitListener {

    /**
     * 进入sixing
     * 消息被拒绝(basic.reject / basic.nack)，并且requeue = false
     * 消息TTL过期
     * 队列达到最大长度
     * @param message
     * @param channel
     */
    @RabbitListener(queues = "test.queue",containerFactory = "directRabbitListenerContainerFactory")
    public void getMessage(Message message, Channel channel) {
        try {
            System.out.println("normal:" + new String(message.getBody()));
            channel.basicReject(message.getMessageProperties().getDeliveryTag(), false);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @RabbitListener(queues = "test.dead.queue",containerFactory = "directRabbitListenerContainerFactory")
    public void getDeadMessage(Message message, Channel channel) {
        try {
            System.out.println("dead:" + new String(message.getBody()));
            channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}
